import 'package:get/get.dart';
import 'package:shop/tools/cartServices.dart';

class CartController extends GetxController {
  RxList cartList = [].obs;

  // checkAllBox
  RxBool checkAllBox = false.obs;

  // 选中商品的总价格
  RxnNum selectTotalPrice = RxnNum(0);

  @override
  void onInit() {
    super.onInit();
    // CartServices.clearCartData();
    // getCartListData();
  }

  @override
  void onClose() {
    super.onClose();
  }

  getCartListData() async {
    var tempList = await CartServices.getCartListData();
    cartList.value = tempList;
    checkAllBox.value = isCheckedAll();
    selectTotalPrice.value = selectPrice();
    update();
  }

  //   增加数量
  void incCartNum(cartItem) {
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]['_id'] == cartItem['_id'] &&
          cartList[i]['selectedAttr'] == cartItem['selectedAttr']) {
        cartItem['count']++;
      }
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartServices.setCartListData(tempList);
    // 数量变化后，若商品已选中，需要更新总价
    selectTotalPrice.value = selectPrice();
    update();
  }

  //   减少数量
  void decCartNum(cartItem) {
    var tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]['_id'] == cartItem['_id'] &&
          cartList[i]['selectedAttr'] == cartItem['selectedAttr']) {
        if (cartList[i]['count'] > 1) {
          cartItem['count']--;
        } else {
          Get.snackbar('提示', '购物车数量已经最小了');
        }
      }
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartServices.setCartListData(tempList);
    // 数量变化后，若商品已选中，需要更新总价
    selectTotalPrice.value = selectPrice();
    update();
  }

  //   选中/取消选中购物车商品
  selectCartShop(cartItem) {
    List tempList = [];
    for (var i = 0; i < cartList.length; i++) {
      if (cartList[i]['_id'] == cartItem['_id'] &&
          cartList[i]['selectedAttr'] == cartItem['selectedAttr']) {
        cartList[i]['checked'] = !cartList[i]['checked'];
      }
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartServices.setCartListData(tempList);
    // 选中或 取消选中
    checkAllBox.value = isCheckedAll();
    // 计算价格
    selectTotalPrice.value = selectPrice();

    update();
  }

  //   全选/反选 商品
  void selectAllShop(value) {
    List tempList = [];
    checkAllBox.value = value;
    for (var i = 0; i < cartList.length; i++) {
      cartList[i]['checked'] = value;
      tempList.add(cartList[i]);
    }
    cartList.value = tempList;
    CartServices.setCartListData(tempList);
    // 计算所有选中/未选中价格
    selectTotalPrice.value = selectPrice();
    update();
  }

  //   判断是否全选
  bool isCheckedAll() {
    if (cartList.isNotEmpty) {
      for (var i = 0; i < cartList.length; i++) {
        if (cartList[i]['checked'] == false) {
          return false;
        }
      }
      return true;
    }
    return false;
  }

  //   计算购物车选中总价格
  num selectPrice() {
    num price = 0;

    if (cartList.isNotEmpty) {
      for (var i = 0; i < cartList.length; i++) {
        if (cartList[i]['checked']) {
          price += cartList[i]['price'] * cartList[i]['count'];
        }
      }
    }
    return price;
  }
}
